# range()

print(list(range(10)))
print(list(range(3, 10)))
print(list(range(3, 10, 3)))
for i in range(3, 10):
    print(i)

# 列表推导  生成器 迭代器

# list1 = [1, 2, 3, 4]
# print(list1)

# 根据规则生成一个列表
# [变量 for 表达式 if条件]
list2 = [x for x in range(1, 5) if x % 2 == 0]
print(list2)
print(type(list2))

list3 = (x for x in range(1, 5) if x % 2 == 0)
print(list3)  # <generator object <genexpr> at 0x00000000021D0A40>
print(type(list3))  # <class 'generator'>

print(list(list3))


# for i in list3:
#     print(i)

#  yield

def fun():
    i = 0
    while i < 5:
        i += 1
        yield i


g = fun()

print(g)  # <generator object fun at 0x0000000001E90AF0>

print(list(g))

# 可迭代对象  容器
list4 = (1, 2, 3, 4, 5)
d = iter(list4)
print(type(d))
print(d)

print(next(d))
print(next(d))
print(next(d))
print(next(d))
print(next(d))
# print(next(d))

# print(next(list3))  生成器是一种特殊的迭代器,
g1 = (x for x in range(1, 5) if x % 2 == 0)
# print(list(g1)) # 遍历了一边
# print(next(g1))
# print(next(g1))

for var in g1:
    print(var)

list6 = (1, 2, 3, 4, 5)
d1 = iter(list6)
for var in d1:
    print(var)


# 闭包

def outer():
    x = 0

    def inner():
        nonlocal x
        x += 1
        # print(x)
        return x

    return inner


# 闭包的一个特性
print('*' * 20)
print(type(outer()))
# outer() == inner
res = outer()
print(res())
print(res())
print(res())
